package com.jnshu.web;

import com.jnshu.dto.admin.*;
import com.jnshu.service.AdminService;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.ResultBean;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

@Validated
@Log
@RestController
public class AdminController {

    //编译器报错，无视。 因为这个Bean是在程序启动的时候注入的，编译器感知不到，所以会报错
    @Autowired(required = false)
    AdminService adminService;

    @GetMapping(value = "/manage/list")
    public PageBean managerList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                @RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
        log.info ("_____________________________________________");
        log.info ("方法managerList进来的参数为" +","+pageSize+","+pageNum);
        return adminService.manageList(pageSize,pageNum);
    }

    @DeleteMapping(value = "/manage/{id}")
    public ResultBean deleteManage(@PathVariable("id") @Min(value = 0,message = "id必须大于0")long id){
        log.info ("_____________________________________________");
        log.info("方法deleteManager进来的参数为:"+id);
        return adminService.deleteManage(id);
    }

    @PutMapping(value = "/manage/",produces = {"application/json;charset=UTF-8"})
    public ResultBean updateManage(@RequestBody ManageVo manageVo){
        log.info ("_____________________________________________");
        log.info("方法updateManage进来的参数为："+manageVo);
        return adminService.updateManage(manageVo);
    }

    @PostMapping(value = "/manage",produces = {"application/json;charset=UTF-8"})
    public ResultBean insertManage(@RequestBody ManageVo manageVo){
        log.info ("_____________________________________________");
        log.info("方法insertManage进来的参数为："+manageVo);
        return adminService.insertManage(manageVo);
    }

    @GetMapping(value = "/manage/role/{id}")
    public PageBean selectManageByRole(@RequestParam(name = "pageSize",defaultValue = "10")int pageSize,
                                       @RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                                       @PathVariable("id")long id){
        log.info ("_____________________________________________");
        log.info("方法insertManage进来的参数为："+id);
        return adminService.selectManageByRole(pageSize,pageNum,id);
    }

    @GetMapping(value = "/manage/{id}")
    public ResultBean selectByManageId(@PathVariable("id")Long id){
        log.info ("_____________________________________________");
        log.info("方法selectByManageId进来的参数为："+id);
        return adminService.selectByManageId(id);
    }

    @GetMapping(value = "/role/list")
    public PageBean roleList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                             @RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
        log.info ("_____________________________________________");
        log.info ("方法managerList进来的参数为" +","+pageSize+","+pageNum);
        return adminService.roleList(pageSize,pageNum);
    }

    @DeleteMapping(value = "/role/{id}")
    public ResultBean deleteRole(@PathVariable("id") @Min(value = 0,message = "id必须大于0")long id){
        log.info ("_____________________________________________");
        log.info("方法deleteRole进来的参数为："+id);
        return adminService.deleteRole(id);
    }

    @PutMapping(value = "/role/",produces = {"application/json;charset=UTF-8"})
    public ResultBean updateRole(@RequestBody RoleVo roleVo){
        log.info("_____________________________________________");
        log.info("方法updateRole进来的参数为："+roleVo);
        return adminService.updateRole(roleVo);
    }

    @PostMapping(value = "/role",produces = {"application/json;charset=UTF-8"})
    public ResultBean insertRole(@RequestBody RoleVo roleVo){
        log.info("_____________________________________________");
        log.info("方法insertRole进来的参数为："+roleVo);
        return adminService.insertRole(roleVo);
    }

    @GetMapping(value = "/user/{id}")
    ResultBean selectRoleById(@PathVariable("id")long id){
        log.info("_____________________________________________");
        log.info("方法selectRoleById进来的参数为："+id);
        return adminService.selectRoleById(id);
    }

    @GetMapping(value = "/model/list")
    public PageBean modelList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                              @RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
        log.info ("_____________________________________________");
        log.info ("方法managerList进来的参数为" +","+pageSize+","+pageNum);
        return adminService.modelList(pageSize,pageNum);
    }

    @DeleteMapping(value = "/model/{id}")
    public ResultBean deleteModel(@PathVariable("id") @Min(value = 0,message = "id必须大于0")long id){
        log.info ("_____________________________________________");
        log.info("方法deleteModel进来的参数为："+id);
        return adminService.deleteModel(id);
    }

    @PutMapping(value = "/model/",produces = {"application/json;charset=UTF-8"})
    public ResultBean updateModel(@RequestBody ModelVo modelVo){
        log.info ("_____________________________________________");
        log.info("方法updateModel进来的参数为："+modelVo);
        return adminService.updateModel(modelVo);
    }

    @PostMapping(value = "/model",produces = {"application/json;charset=UTF-8"})
    public ResultBean insertModel(@RequestBody ModelVo modelVo){
        log.info ("_____________________________________________");
        log.info("方法insertModel进来的参数为："+modelVo);
        return adminService.insertModel(modelVo);
    }

    @PutMapping(value = "/manage/password",produces = {"application/json;charset=UTF-8"})
    public ResultBean updateManagePassword(@RequestBody PasswordVo passwordVo){
        log.info ("_____________________________________________");
        log.info("方法updateManagePassword进来的参数为："+passwordVo);
        return adminService.updateManagePassword(passwordVo);
    }
}
